<?php

namespace app\admin\controller\qingdongwms\finance;

use app\admin\controller\qingdongwms\Base;
use addons\qingdongwms\model\Account as AccountModel;


/**
 * 审批管理
 * @icon fa fa-user
 */
class Account extends Base {
	protected $relationSearch = true;
	/**
	 * @var \addons\qingdongwms\model\Account
	 */
	protected $model = null;


	public function _initialize() {
		parent::_initialize();
		$this->model = new AccountModel();
	}


	/**
	 * 查看
	 */
	public function index() {
		//设置过滤方法
		$this->request->filter(['strip_tags', 'trim']);
		if ($this->request->isAjax()) {
			//如果发送的来源是Selectpage，则转发到Selectpage
			if ($this->request->request('keyField')) {
				return $this->selectpage();
			}
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->where($where)->where([])->order('id desc')->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
		}

		return $this->view->fetch();
	}

    //添加
    public function add() {
        if ($this->request->isAjax()) {
            $data             = $this->request->post('row/a');
            $result           = $this->model->save($data);
            if (!$result) {
                $this->error('提交失败');
            }
            $this->success('提交成功');
        }

        return $this->view->fetch();
    }


    //修改
    public function edit($ids = null) {
        $map['id'] = $ids;
        if ($this->request->isAjax()) {
            $data   = $this->request->post('row/a');
            $result = $this->model->save($data, $map);

            if (!$result) {
                $this->error('修改失败');
            }
            $this->success('修改成功');
        }
        $data = AccountModel::where($map)->find();
        $this->view->assign("row", $data);

        return $this->view->fetch();
    }
}
